package cn.iocoder.yudao.module.member.user.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.business.basic.pojo.vo.UserSimpleVO;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.api.member.user.MemberUserApi;
import cn.iocoder.yudao.module.member.user.dal.dataobject.MemberUserDO;
import cn.iocoder.yudao.module.member.user.service.MemberRegisterService;
import cn.iocoder.yudao.module.member.user.service.MemberUserService;
import jakarta.annotation.Nullable;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.api.member.ErrorCodeConstants.USER_MOBILE_NOT_EXISTS;

/**
 * 前台用户的 API 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class MemberUserApiImpl implements MemberUserApi {

    @Resource
    private MemberUserService memberUserService;
    @Resource
    private MemberRegisterService memberRegisterService;

    @Override
    public Long getInviterId(Long id) {
        return memberUserService.getInviterId(id);
    }

    @Override
    public Set<Long> getInviteeIds(Long id) {
        return memberUserService.getInviteeIds(id);
    }

    @Override
    public UserSimpleVO getUser(Long id) {
        return memberUserService.getUserFromCache(id);
    }

    @Override
    public String getUserMobile(Long id) {
        return memberUserService.getUserMobile(id);
    }

    @Override
    public List<UserSimpleVO> getUserSimpleList(Collection<Long> ids) {
        List<MemberUserDO> list = memberUserService.getUserSimpleList(ids);
        return BeanUtils.toBean(list, UserSimpleVO.class);
    }

    @Override
    public List<UserSimpleVO> getUserListByNickname(String nickname) {
        List<MemberUserDO> list = memberUserService.getUserListByNickname(nickname);
        return BeanUtils.toBean(list, UserSimpleVO.class);
    }

    @Override
    public UserSimpleVO getUserByMobile(String mobile) {
        MemberUserDO user = memberUserService.getUserByMobile(mobile);
        return BeanUtils.toBean(user, UserSimpleVO.class);
    }

    @Override
    public UserSimpleVO validateUser(Long id) {
        MemberUserDO user = memberUserService.getUser(id);
        if (user == null) {
            throw exception(USER_MOBILE_NOT_EXISTS);
        }
        return BeanUtils.toBean(user, UserSimpleVO.class);
    }

    @Override
    public Long createRobot(String nickname, String avatar) {
        return memberUserService.createRobot(nickname, avatar).getId();
    }

    @Nullable
    @Override
    public UserSimpleVO getUserFromCache(Long id) {
        return memberUserService.getUserFromCache(id);
    }

    @Nullable
    @Override
    public Set<Long> getUserIdsByMobileAndNick(String mobile, String nick) {
        Set<Long> userIds = new HashSet<>();
        if (PhoneUtil.isMobile(mobile)) {
            MemberUserDO user = memberUserService.getUserByMobile(mobile);
            if (user == null) {
                return null;
            }
            userIds.add(user.getId());
        } else if (StrUtil.isNotEmpty(mobile)) {
            List<MemberUserDO> users = memberUserService.getUserListByMobile(mobile);
            if (CollUtil.isEmpty(users)) {
                return null;
            }
            userIds.addAll(CollectionUtils.convertSet(users, MemberUserDO::getId));
        }
        if (StrUtil.isNotEmpty(nick)) {
            List<MemberUserDO> users = memberUserService.getUserListByNickname(nick);
            if (CollUtil.isEmpty(users)) {
                return null;
            }
            userIds.addAll(CollectionUtils.convertSet(users, MemberUserDO::getId));
        }
        return userIds;
    }

    @Override
    public boolean bindRelation(Long userId, String inviteCode) {
        return memberRegisterService.bindRelation(userId, inviteCode);
    }

    @Override
    public boolean bindRelation(Long userId, Long inviteUserId) {
        return memberRegisterService.bindRelation(userId, inviteUserId);
    }

}
